/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 *
 * This software is distribuable under the GNU Lesser General Public License.
 * For more information visit gnu.org.
 */
package org.emergent.antbite.savant;


import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;


/**
 * <p>
 * This class is the implementation of the URLBuilder interface
 * for Savant URLs.
 * </p>
 *
 * <p>
 * Savant's URL scheme is
 * <b>domain</b>/<b>group</b>/<b>project</b>/<b>name</b>-<b>version</b>.<b>type</b>
 * </p>
 *
 * @author Brian Pontarelli
 */
public class SavantURLBuilder extends AbstractURLBuilder {

    /**
     * Builds the URL to the artifact using Savant style URLs.
     *
     * @param   defaultDomain The default domain to use while building the URL
     * @param   mapping (optional) Mapping file that might override the default
     *          domain URL
     * @param   artifact The artifact to build the URL for
     * @return  The URL or null if it couldn't be built (default domain was null
     *          or something)
     * @throws  SavantException If the URL could not be built
     */
    public URL buildURL(String defaultDomain, File mapping, Artifact artifact)
    throws SavantException {
        String baseURL = makeBaseURLSpec(defaultDomain, mapping, artifact);
        URL url = null;
        if (baseURL != null) {
            String urlSpec = baseURL + "/" + makeURLSpec(artifact);
            try {
                url = new URL(urlSpec);
            } catch (MalformedURLException mue) {
                throw new SavantException(mue);
            }
        }

        return url;
    }

    /**
     * Builds the URL to the MD5 for the artifact using Savant style URLs.
     *
     * @param   defaultDomain The default domain to use while building the URL
     * @param   mapping (optional) Mapping file that might override the default
     *          domain URL
     * @param   artifact The artifact to build the URL for
     * @return  The URL or null if it couldn't be built (default domain was null
     *          or something)
     * @throws  SavantException If the URL could not be built
     */
    public URL buildMD5URL(String defaultDomain, File mapping, Artifact artifact)
    throws SavantException {
        String baseURL = makeBaseURLSpec(defaultDomain, mapping, artifact);
        URL url = null;
        if (baseURL != null) {
            String urlSpec = baseURL + "/" + makeURLSpec(artifact) + ".md5";
            try {
                url = new URL(urlSpec);
            } catch (MalformedURLException mue) {
                throw new SavantException(mue);
            }
        }

        return url;
    }

    /**
     * Attempts to builds a URL to the XML dependency meta-data for the given
     * artifact.
     *
     * @param   artifact The artifact to build the URL for
     * @param   defaultDomain The default domain URL specification (base URL)
     * @param   mapping A Mapping file that can be used to map groups to base URLs
     *          to override the default
     * @return  The XML dependency URL or null if one could not be built
     */
    public URL buildDepsURL(String defaultDomain, File mapping,
            Artifact artifact)
    throws SavantException {
        String baseURL = makeBaseURLSpec(defaultDomain, mapping, artifact);
        URL url = null;
        if (baseURL != null) {
            String urlSpec = baseURL + "/" + makeURLSpec(artifact) + ".deps";
            try {
                url = new URL(urlSpec);
            } catch (MalformedURLException mue) {
                throw new SavantException(mue);
            }
        }

        return url;
    }

    /**
     * Makes the URL additions specifically for Savant URLs.
     *
     * @param   artifact The artifact to build the URL for
     * @return  The url and never null
     */
    private String makeURLSpec(Artifact artifact) {
        String url = artifact.getGroup() + "/" + artifact.getProjectname() + "/" +
            artifact.getName();
        if (artifact.getVersion() != null) {
            url = url + "-" + artifact.getVersion();
        }

        url = url + "." + artifact.getType();

        return url;
    }
}
